<template>
  <div>
    <div id="demo-container">
      <div id="mount-point"></div>
      <div id="plugin-point"></div>
    </div>
  </div>
</template>

<script>
import { Enqueue,StartProject,Dequeue,heartbeat,StopProject } from '@/api/cloud'
export default {
  data () {
    return {
      form: {
        ProjectId: 'cap-g6q7buoh',
        ApplicationId: 'app-abdbhcrm'
      },
      times: null,
      headers: {
        Authorization: ''
      }
    }
  },
  destroyed () { // 销毁--取消排队、停止云渲染和心跳
    this.getDequeue()
    this.getStopProject()
    clearInterval(this.times)
  },
  created () {
    this.headers.Authorization = this.$route.query.Authorization
    this.times = setInterval(() => {
      this.getHeartbeat()
    }, 1000 * 15) // 15s一次心跳
    // 先取消排队
    this.getStopProject()
    // 再加入排队再启动
    this.getEnqueue()
  },
  methods: {
    // 启动
    getStart() {
      const formData = new FormData()
        formData.set('ProjectId', this.form.ProjectId)
        formData.set('ApplicationId', this.form.ApplicationId)
        formData.set('ClientSession', TCGSDK.getClientSession())
        if(!this.headers.Authorization){
          return 
        }
        StartProject(formData,this.headers).then(res => {
        	if(res.code == 0) {
            TCGSDK.start(res.data.ServerSession)
          }
        })
    },
    // 页面渲染
    getInfo() {
      let lastX = null;
      let lastY = null;
      TCGSDK.init({
        appid: '1323110658',
        mount: 'mount-point',
        debugSetting: {
          showLog: true,
        },
        // Connected successfully.
        onConnectSuccess: async (res) => {
          console.log('onConnectSuccess', res);
          // Add joystick
          const j = new CloudGamingPlugin.joystick({
            zone: document.querySelector('#plugin-point'),
          });
        },
        // The network was disconnected or the user was kicked out.
        onDisconnect: (res) => {
          console.log('onDisconnect', res);
        },
        onWebrtcStatusChange: (res) => {
          console.log('onWebrtcStatusChange', res);
        },
        onInitSuccess: async (res) => {
          console.log('%c onInitSuccess', 'color: red', res);
          await this.getStart();
        }
      });
      
    },
    // 加入排队
    getEnqueue() {
      Enqueue(this.form,this.headers).then(res => {
        // 排队中
          // if (res.code === 10100) {
          //   // 5s 轮训一次队列
          //   setTimeout(async () => {
          //     await this.getEnqueue();
          //   }, 5000);
          // }
          
          // 排队成功
          if (res.code === 10100 || res.code === 10101) {
             this.getInfo();
          }
        })
    },
    //取消排队
    getDequeue(){
      Dequeue(this.form,this.headers).then(res => {
      	
      })
    },
    // 心跳接口
    getHeartbeat(){
      heartbeat(this.form,this.headers).then(res => {
      	
      })
    },
    // 停止云渲染
    getStopProject(){
      StopProject(this.form,this.headers).then(res => {
      	
      })
    }
  }
}
</script>

<style scoped>
  * {
    padding: 0;
    margin: 0;
  }
  
  html,
  body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: 'SimHei', 'Microsoft YaHei', 'Arial', 'sans-serif';
  }
  
  #demo-container {
    width: 100%;
    height: 100%;
  }
  
  #plugin-point {
    position: absolute;
    left: 100px;
    bottom: 150px;
    width: 0px;
    height: 0px;
  }
</style>
